package com.jintoufs.service.sys.impl;

import com.jintoufs.dao.QueryFilters;
import com.jintoufs.dao.sys.DictionaryDAO;
import com.jintoufs.domain.sys.Dictionary;
import com.jintoufs.service.sys.DictionaryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
public class DictionaryServiceImpl implements DictionaryService {
    @Resource
    private DictionaryDAO dictionaryDAO;

    @Override
    public List<Dictionary> getByFilter(QueryFilters filters) {
        return dictionaryDAO.getByFilter(filters);
    }

    @Override
    public Integer update(Dictionary entity) {
        return dictionaryDAO.update(entity);
    }

    @Override
    public Dictionary insert(Dictionary entity) {
        dictionaryDAO.insert(entity);
        return entity;
    }

    @Override
    public void delete(Integer id) {
        dictionaryDAO.delete(id);
    }

    @Override
    public List<Dictionary> getDicByKeyNo(String keyNo) {
        return dictionaryDAO.getDicByKeyNo(keyNo);
    }

    @Override
    public List<Dictionary> getByParams(Map<String, Object> params1) {
        return dictionaryDAO.getByParams(params1);
    }

    @Override
    public List<Dictionary> queryPlatformsByTaskId(Long taskId) {
        return dictionaryDAO.queryPlatformsByTaskId(taskId);
    }

    @Override
    public Map<String, LinkedHashMap<String, String>> getdicMap() {
        QueryFilters filters=new QueryFilters();
        List<Dictionary> list = dictionaryDAO.getByFilter(filters);
        Map<String, LinkedHashMap<String, String>> dicMap = new HashMap<String, LinkedHashMap<String, String>>();
        LinkedHashMap<String, String> onedic = null;
        for (int i = 0, size = list.size(); i < size; i++) {
            Dictionary dictionary = list.get(i);
            if (!dicMap.containsKey(dictionary.getKeyNo().toString())) {
                onedic = new LinkedHashMap<String, String>();
                onedic.put(dictionary.getKeyValue(), dictionary.getCaption());
                dicMap.put(dictionary.getKeyNo().toString(), onedic);
            } else {
                onedic.put(dictionary.getKeyValue(), dictionary.getCaption());
            }
        }
        return dicMap;
    }

    @Override
    public String getAccessToken(String keyNo) {
        String token = "";
        List<Dictionary> list = this.getDicByKeyNo(keyNo);
        if (list != null && list.size() > 0) {
            token = list.get(0).getCaption();
        }
        return token;
    }
}
